/*
 * AisleNav Android Application
 * DeleteItemListActivity.java
 * project.AisleNav.ListActivity
 * 
 * CS495
 * 
 * Team:
 * 		Kirby Green
 * 		Mark Hale
 * 		Adam Warnock
 * 
 * Time: 6:14:34 PM
 * Date: Dec 4, 2011
 * 
 */
package project.AisleNav.ListActivity;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

// TODO: Auto-generated Javadoc
/**
 * ListViewText.java Sample ListView to test
 * 
 * @author Kirby Green
 * @version 0.1
 * 
 */
public class DeleteItemListActivity extends ListActivity {
	
	/** The alert. */
	private AlertDialog alert;
	
	/** The selected list. */
	private long selectedList = -1;
	
	/**
	 * Called when the activity is first created.
	 *
	 * @param savedInstanceState the saved instance state
	 * @author Kirby Green
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//QUICK TEST
		if(project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems() != null) {
		int s = project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems().size();
		System.out.println("Test - Beginning Size: " + s);
		project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems().clear();
		s = project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems().size();
		System.out.println("Test - Cleared Size: " + s);
		project.AisleNav.AisleNavActivity.caller.load();
		s = project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems().size();
		System.out.println("Test - Loaded Size: " + s);
		}
		else
			System.out.println("FavoriteItems is null!");
		
		
		
		String[] names = project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems().getAllNames();
		for(int i = 0; i < names.length; ++i)
			System.out.println(names[i]);
		System.out.println("Done loading strings");

		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names));
		
		
		
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		
		AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
		alertBuilder.setMessage("Are you sure you want to delete this list?").setCancelable(false)
			.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(selectedList >= 0) {
						// TODO delete it at
						System.out.println("Deleting list number " + selectedList);
						project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems().removeList((int) selectedList);
						project.AisleNav.AisleNavActivity.caller.save();
						finish();
					}
					
				}
			})
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
					
				}
			});
		
		alert = alertBuilder.create();
		alert.setTitle("Delete?");
		alert.setIcon(android.R.id.icon);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectedList = id;
				showAlert();
			}
		});
		
	}
	
	/**
	 * Show alert.
	 */
	private void showAlert() {
		alert.show();
	}
}
